# Which compiler
# CC = gcc
# AR = ar
# AT = @

ifeq ($(shell uname -o), GNU/Linux)
	OS = OS_LINUX
else
	OS =
endif

# source file
SRCS := user_malloc.c

# LUA_SRCS_PATH = lua_dna
LUA_SRCS_PATH = lua-5.3.0/src

# lua-dna used parts
SRCS += $(LUA_SRCS_PATH)/lapi.c
SRCS += $(LUA_SRCS_PATH)/lauxlib.c
SRCS += $(LUA_SRCS_PATH)/lbaselib.c
SRCS += $(LUA_SRCS_PATH)/lbitlib.c
SRCS += $(LUA_SRCS_PATH)/lcode.c
SRCS += $(LUA_SRCS_PATH)/lcorolib.c
SRCS += $(LUA_SRCS_PATH)/lctype.c
SRCS += $(LUA_SRCS_PATH)/ldblib.c
SRCS += $(LUA_SRCS_PATH)/ldebug.c
SRCS += $(LUA_SRCS_PATH)/ldo.c
SRCS += $(LUA_SRCS_PATH)/ldump.c
SRCS += $(LUA_SRCS_PATH)/lfunc.c
SRCS += $(LUA_SRCS_PATH)/lgc.c
SRCS += $(LUA_SRCS_PATH)/linit.c
SRCS += $(LUA_SRCS_PATH)/llex.c
SRCS += $(LUA_SRCS_PATH)/lmem.c
SRCS += $(LUA_SRCS_PATH)/lobject.c
SRCS += $(LUA_SRCS_PATH)/lopcodes.c
SRCS += $(LUA_SRCS_PATH)/lparser.c
SRCS += $(LUA_SRCS_PATH)/lstate.c
SRCS += $(LUA_SRCS_PATH)/lstring.c
SRCS += $(LUA_SRCS_PATH)/lstrlib.c
SRCS += $(LUA_SRCS_PATH)/ltable.c
SRCS += $(LUA_SRCS_PATH)/ltablib.c
SRCS += $(LUA_SRCS_PATH)/ltm.c
SRCS += $(LUA_SRCS_PATH)/lundump.c
SRCS += $(LUA_SRCS_PATH)/lvm.c
SRCS += $(LUA_SRCS_PATH)/lzio.c

# open in lua-5.3.0
# SRCS += $(LUA_SRCS_PATH)/lmathlib.c
SRCS += $(LUA_SRCS_PATH)/loadlib.c
# SRCS += $(LUA_SRCS_PATH)/lutf8lib.c

# both remove from 5.0 and 5.3
# SRCS += $(LUA_SRCS_PATH)/liolib.c
# SRCS += $(LUA_SRCS_PATH)/loslib.c

SRCS += lua-5.3.0/cjson/lua_cjson.c
SRCS += lua-5.3.0/cjson/strbuf.c
SRCS += lua-5.3.0/cjson/fpconv.c


OBJS = $(patsubst %.c,%.o,$(SRCS))

# LIBS += -lm

# Where are include files kept
INCLUDES := -I./
INCLUDES += -I./$(LUA_SRCS_PATH)
INCLUDES += -I./lua-5.3.0/cjson
INCLUDES += -I$(MIDDLEWARE_PATH)

# Option for development
CFLAGS = -g -Wall
# CXXFLAGS =
# Option for release
# CFLAGS = -O -Wall

CFLAGS += -DLUA_COMPAT_MODULE
CFLAGS += -D$(OS)

TARGET = liblua.a


all: $(OBJS)
	$(AT)$(AR) rcs $(TARGET) $^
	$(AT)mv $(TARGET) $(LIBS_PATH)

$(OBJS):%.o:%.c
	$(AT)$(CC) $(CFLAGS) $(INCLUDES) $(LIBS) -c $^ -o $@


.PHONY: clean
clean:
	$(AT)-rm -f $(OBJS)
